package model.genetic.genes;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SudokuLineGene {
	
	private int[] dna;
	private List<Integer> nonFixedIndices;
	
	SudokuLineGene(int[] dna, List<Integer> nonFixedIndices) {
		this.dna = dna;
		this.nonFixedIndices = Collections.unmodifiableList(nonFixedIndices);
	}
	
	public SudokuLineGene copy() {
		
		return new SudokuLineGene(Arrays.copyOf(dna, dna.length), nonFixedIndices);
	}
	
	public int getValueAt(int i) {
		return dna[i];
	}
	
	public void setValueAt(int index, int value) {
		dna[index] = value;
	}
	
	public int getLength() {
		return dna.length;
	}
	
	public boolean isFixed(int index) {
		return !nonFixedIndices.contains(new Integer(index)); //TODO linear, we know it's sorted, use binarySearch
	}
	
	public List<Integer> getNonFixedIndices() {
		return nonFixedIndices;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof SudokuLineGene))
			return false;
		
		SudokuLineGene other = (SudokuLineGene) obj;
		
		if (getLength() != other.getLength())
			return false;
		
		for (int i = 0; i < getLength(); ++i)
			if (getValueAt(i) != other.getValueAt(i))
				return false;
		
		List<Integer> otherNonFixed = other.getNonFixedIndices();
		if (nonFixedIndices.size() != otherNonFixed.size())
			return false;
		
		for (int i = 0; i < nonFixedIndices.size(); ++i)
			if (!nonFixedIndices.get(i).equals(otherNonFixed.get(i)))
				return false;
		
		return true;
	}
	
}
